<?php

namespace Biz\model\settlement;

use Doraemon\pockets\datebase\BaseDbModel;

class Bills extends BaseDbModel
{

    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_settlement';

    const _DATABASE_ = 'shop_settlement';

    const _TABLE_NAME = 'bills';

    const STATUS_NORMAL = 0;//待确认
    const STATUS_WILL_TRANSFER = 1;//待转账
    const STATUS_TRANSFER_SUCCESS = 2;//已转账

    const CHANNEL_ALL = 0;//全部，1,2,3,4的总计
    const CHANNEL_ALIPAY = 1;
    const CHANNEL_WECHAT = 2;
    const CHANNEL_OTHER = 3;
    const CHANNEL_CUSTOMIZE = 4;

    public static $statusDesc = [
        self::STATUS_NORMAL => '待商家确认',
        self::STATUS_WILL_TRANSFER => '待财务转账',
        self::STATUS_TRANSFER_SUCCESS => '处理完成',
    ];

    public static $channelDesc = [
        self::CHANNEL_ALL => '全部',
        self::CHANNEL_ALIPAY => '支付宝',
        self::CHANNEL_WECHAT => '微信',
        self::CHANNEL_CUSTOMIZE => '线下支付',
        self::CHANNEL_OTHER => '其他'
    ];

    public static function addData($data)
    {
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            throw new \Exception('数据库操作错误');
        }
        return true;
    }

    public static function insertData($data)
    {
        $ret = self::insert($data);
        return self::id();
    }

    public static function updateData($data, $where)
    {
        $ret = self::update($data, $where);
        return $ret->rowCount();
    }

    public static function changeBills($data, $where)
    {
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            throw new \Exception('数据库操作错误');
        }
        return true;
    }

    public static function hasChannel($shopId, $billDate, $channel)
    {
        return self::has(['shop_id' => $shopId, 'bill_date' => $billDate, 'channel' => $channel]);
    }


    public static function getBillList($select = '*', $where, $limit = [], $order = [])
    {
        $params['AND'] = $where;
        if ($limit) {
            $params['LIMIT'] = $limit;
        }
        if ($order) {
            $params['ORDER'] = $order;
        }
        return self::select($select, $params);
    }

    public static function getBillListCount($where)
    {
        return self::count('*', $where);
    }

}

